<?php
IncludeFunction("tourtype");
IncludeFunction("location");

function ListTour($idP=0,$status=-1,$loc=-1,$search='')
{
    global $dbsql,$language;

    $sql_where  = "from tblsettour where cLanguage='$language' ";
    if($idP>0)
    {
        $sql_where.= " and (iParentId = $idP or iParentId2 =$idP) ";
    }
    if($status>-1)
    {
        $sql_where.= " and iStatus   = $status ";
    }
    if($loc>-1)
    {
        $cid       = ListIdTourByListLocationId($loc);
        $sql_where.= " and iId in ($cid) ";
    }
    if(!empty($search))
    {
        $sql_where.= " and cName like '$search%' ";
    }
    //----------split page
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $count_all  = $result['count'];
    list($limit_st,$limit_le)=Slit_Page($count_all);

    //--------------------------------------------------
    $sql_select = "select * ".$sql_where." order by iOrder limit $limit_st,$limit_le";
    return $dbsql->load($sql_select);

}

function ListIdTourByListLocationId($idloc,$returnarray=false)
{
    global $dbsql,$language;
    if(is_array($idloc))
    {
        $idloc  = implode(',',$idloc);
    }
    $list       = array();
    $sql_select = "select iTourId from tblsettour_location where iLocationId in (".$idloc.")";
    $rs         = $dbsql->query($sql_select);
    while($result   = $dbsql->fetch_array($rs))
    {
        $list[]     = $result['iTourId'];
    }
    if($returnarray==false)
    {
        $list   = implode(',',$list);
        if(empty($list))
        {
            $list   = '-1';
        }
    }
    return $list;
}

function LoadTourById($id)
{
    global $dbsql,$language;

    $sql_select = "select * from tblsettour where iId=$id";
    return $dbsql->query_fetch($sql_select);
}
//tour element:

function ListTourElement($idP)
{
    global $dbsql,$language;
    $idP    = intval($idP);
    $sql_select="select * from tblsettourelm where iParentId = $idP order by iOrder ";
    return $dbsql->load($sql_select);
}

function ListTabsByTour($id)
{
    global $dbsql,$language;

    $sql_select = "select * from tbltourtab where iParentId=$id order by iOrder";
    return $dbsql->load($sql_select);
}

function DelTour($id)
{
global $dbsql,$language;

    $id         = intval($id);
    $sql_delete = "DELETE FROM tblsettour WHERE iId ='$id'";
    return $dbsql->query($sql_delete);
}
function DelTourElmByTourId($idP)
{
    global $dbsql,$language;

    $idP        = intval($idP);
    $sql_delete = "DELETE FROM tblsettourelm where iParentId=$idP";
    return $dbsql->query($sql_delete);
}

function DelTourTabByTourId($idP)
{
    global $dbsql,$language;

    $idP        = intval($idP);
    $sql_select = "SELECT * FROM tbltourtab WHERE iParentId = $idP";
    $result     = $dbsql->query_fetch($sql_select);
    $id         = intval($result['iId']);
    if($result['cType']=="Image")
    {
        $sql_delete = "delete from tblgallery where iParentId=$id and cType='tourtab' ";
        $dbsql->query($sql_delete);
    }
    $sql_delete = "DELETE FROM tbltourtab WHERE iId = $id";
    return $dbsql->query($sql_delete);
}

function ListPhotoTour($id)
{
    global $dbsql,$language;
    
    $id         = intval($id);
    $sql_select = "select * from tblgallery where cType='tour' and iParentId=$id order by iOrder";
    return $dbsql->load($sql_select);
}

function UpdateMapLocationId($listid,$idtour)
{
    global $dbsql,$language;
    $idtour     = intval($idtour);
    if($idtour==0)
    {
        return;
    }
    $sql_delete = "DELETE FROM tblsettour_location where iTourId=".$idtour;
    $dbsql->query($sql_delete);
    foreach($listid as $idloc)
    {
        $sql_insert = "insert into tblsettour_location (iTourId,iLocationId)
                        values('$idtour','$idloc')";
        $dbsql->query($sql_insert);
        
    }
}

function ListVideoTour($id)
{
    global $language,$dbsql;

	$sql_select = "SELECT * FROM tblsettour_video WHERE iParentId = '$id' order by iOrder ";
    return $dbsql->load($sql_select);
}

function getTourIdByName($search)
{
    global $dbsql,$language;
    
    $search     = trim($search);
    $search_url = Trim_Url($search);
    
    $sql_select="select iId,cName from tblsettour where (cName like '$search')
			and cLanguage='$language' limit 1";
	$rs =  $dbsql->query_fetch($sql_select);
    if($rs)
    {
        return $rs['iId'];
    }else
    {
        return 0;
    }
}

function ListOptionTour($select)
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cName from tblsettour order by cName ";
    $rs         = $dbsql->query($sql_select);
    
    $options        = '';
    while($result   = $dbsql->fetch_array($rs))
    {
        if($result['iId'] == $select)
        {
            $options .= '<option value="'.$result['iId'].'" selected>'.$result['cName'].'</option>';
        }else
        {
            $options .= '<option value="'.$result['iId'].'">'.$result['cName'].'</option>';
        }
        
    }
    return $options;
}
?>